<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

require_once(dirname(__FILE__).DIR_SEP."vsdateedit.php");

require_once(DIR_LIB."Junona".DIR_SEP."junobjectsfactory.php");
require_once(DIR_LIB."Junona".DIR_SEP."junoperatorwork.php");

class XCartOperations extends VsSmartyWidget {

	private $_products = array();

	function __construct(&$page, $modelId, $pager=NULL) {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		$this->_template = dirname(__FILE__).DIR_SEP."xcart_operations.html";
		$this->_wwwDir = 'templates/XCart/';

		$page->addJsFile("js/jquery-1.7.2.min.js");

		$minDate = isset($_POST['fromVsdeDate']) ? $_POST['fromVsdeDate'] : date("Y-m-d");
		$maxDate = isset($_POST['toVsdeDate']) ? $_POST['toVsdeDate'] : date("Y-m-d");
		

		$fromDateEdit = new VsDateEdit($page, $minDate);
		$this->assign('fromDateEdit',  $fromDateEdit->show());
		$this->assign('fromVsdeDate',  $fromDateEdit->date());

		$toDateEdit = new VsDateEdit($page, $maxDate);
		$this->assign('toDateEdit',  $toDateEdit->show());
		$this->assign('toVsdeDate',  $toDateEdit->date());

		if(isset($_POST['sub_operation'])) {
			$this->procForm();
		}

		if(isset($_POST['mode']) && ($_POST['mode']=='change_names')) {
			//echo $_POST['mode']; exit;
			$this->procFormOperationNames();
		}

		if(isset($_POST['sub_operations'])) {
			$this->procFormOperations();
		}

		if(isset($_POST['del_operations'])) {
			$this->procFormDelete();
		}
		
		$model = JunObjectsFactory::makeModel($modelId);
		$operations = JunObjectsFactory::makeOperations($modelId);

		$this->assign('model', $model);
		$this->assign('listOperations', $operations[0]);
		$this->assign('modelPrice', $operations[1]);
		$this->assign('modelMinutes', $operations[2]);

		if(isset($_REQUEST['opid'])) {
			$this->assign('operator', JunObjectsFactory::makeOperator($_REQUEST['opid']));

			$dateEdit = new VsDateEdit($page);
			$this->assign('dateEdit', $dateEdit->show());

			$this->assign('vsdeDate', $dateEdit->date());

			if(isset($_POST['submitAddWork']))
				$this->procFormAddWork();
		}

		
		$settings = JunObjectsFactory::makeSettings();
		$this->assign('settings', $settings);
		
		if($pager)
			$this->assign('pager', $pager->show());
		else
			$this->assign('pager', '');
	}
	//============================================================================

	function show() {
		$this->assign('wwwDir', $this->_wwwDir);

		return $this->fetch($this->_template);
	}
	//============================================================================

	private function procForm() {
		$model = JunOperation::makeFromForm($_POST);
		$model->save();

		header("Location: ".$_SERVER['PHP_SELF']."?modid=".$_REQUEST['modid']);
	}
	//===========================================================================

	private function procFormOperations() {
		//print_r($_POST['posted_data']);
		foreach($_POST['opids'] as $id=>$value) {
			JunOperation::changePrice($id, $_POST['posted_data'][$id]);

			if(isset($_POST['change_work'])) {
				//echo "Change Work From:"; echo $_POST['fromVsdeDate']; exit;

				JunOperation::updateWorkPrices($id, $_POST['posted_data'][$id]['price'], $_POST['fromVsdeDate'], $_POST['toVsdeDate']);
				
			}
		}

		header("Location: ".$_SERVER['PHP_SELF']."?modid=".$_REQUEST['modid']);
	}
	//===========================================================================

	private function procFormDelete() {
		//echo "EHO"; exit;
		foreach($_POST['opids'] as $id=>$value) {
			JunOperation::remove($id);
		}

		//exit;
		header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
	}
	//===========================================================================

	private function procFormOperationNames() {
		//print_r($_POST['posted_data']);
		foreach($_POST['opids'] as $id=>$value) {
			JunOperation::updateNames($id, $_POST['posted_data'][$id]['number'], $_POST['posted_data'][$id]['name']);
		}

		header("Location: ".$_SERVER['PHP_SELF']."?modid=".$_REQUEST['modid']);
	}
	//===========================================================================

	private function procFormAddWork() {
		$work = JunOperatorWork::makeForAdd($_POST['opid'], $_POST['vsdeDate']);

		foreach($_POST['opids'] as $id=>$value) {
			$op = JunObjectsFactory::makeOperation($id);

			$work->addOperation($op, $_POST['posted_data'][$id]['count'], $_POST['vsdeDate']);

			//$p->setPrice($_POST['posted_data'][$id]['price']);
		}

		$work->save();

		header("Location: ".$_SERVER['PHP_SELF']."?opid=".$_REQUEST['opid']);		//."?modid=".$_REQUEST['modid']);
	}
	//============================================================================

};

?>
